<?php
/**
 * Created by PhpStorm.
 * User: kamlyli
 * Date: 2017/8/11
 * Time: 20:29
 */

namespace app\api\controller\v1;


use app\api\model\Examination;
use app\api\model\ExaminationDetail;
use app\api\model\ExaminationRecord;
use app\api\model\ProblemDetail;
use app\api\model\Mistake;

use think\Request;

class Exam extends Base
{
    public function get_confirm($type)
    {
        // 检查是不是这个人
        $this->check_cookie();

        // 校验数据 type

        $exam = new Examination();
        $exam = $exam->where(['userId' => $this->userId, 'type' => $type])->find();
        if (!$exam) {
        }

        if ($exam['complete'] > 0) {
            return json_info('上次测试还没有结束');
        } else {
            return json_info('需要重新生成题目');
        }
    }

    public function random_problem($type)
    {
        // 检查是不是这个人
        $this->check_cookie();

        // 校验数据 type

        $exam = new Examination();
        $exam = $exam->where(['userId' => $this->userId, 'type' => $type])->find();
        if (!$exam) {
            $exam = new Examination();
            $exam->userId = $this->userId;
            $exam->type = $type;
            $exam->save();
        } else {
            $exam->examId = $exam->examId + 1;
            $exam->complete = 1;
            $exam->isUpdate(true)->save();
        }

        $exam = new Examination();
        $exam = $exam->where(['userId' => $this->userId, 'type' => $type])->find()->toArray();
        self::_random_problem($exam['examId'], $type);
        return json_info('生成成功');
    }

    private function _random_problem($examId, $type)
    {
        $problem_detail = new ProblemDetail();
        $problem_detail = $problem_detail->where(['type' => $type])->select()->toArray();
        $tempId = array_rand($problem_detail, config('params.EXAMNUMBER'));

        for ($i = 0; $i < config('params.EXAMNUMBER'); $i++) {
            $exam_detail = new ExaminationDetail();
            $exam_detail->userId = $this->userId;
            $exam_detail->problemDetailId = $problem_detail[$tempId[$i]]['id'];
            $exam_detail->examId = $examId;
            $exam_detail->examProblemId = $i + 1;
			$exam_detail->type = $type;
            $exam_detail->save();
        }
    }

    public function get_question($type)
    {
        $this->check_cookie();

        // 校验数据 type

        $exam = new Examination();
        $exam = $exam->where(['userId' => $this->userId, 'type' => $type])->find()->toArray();
        if (!$exam) {

        }

        $exam_detail = new ExaminationDetail();
        $exam_detail = $exam_detail->where(['userId' => $this->userId, 'examId' => $exam['examId'], 'type' => $type])->select()->toArray();
        if (!$exam_detail) {

        }

        for ($i = 0; $i < count($exam_detail); $i++) {
            $problem_detail = new ProblemDetail();
            $problem_detail = $problem_detail->where(['id' => $exam_detail[$i]['problemDetailId']])->field(['id', 'type', 'des', 'img', 'optionA', 'optionB', 'optionC', 'optionD'])->find()->toArray();
            $exam_detail[$i]['problem_detail'] = $problem_detail;
        }

		$message['exam_detail'] = $exam_detail;
		$message['examId'] = $exam['examId'];

        return json_info($message);
    }

    public function answer_question(Request $request)
    {
        // 检查是不是这个人
        $this->check_cookie();

        // 校验数据  id , answer,

        // 获取   id , answer,
        $param = $request->param();

        $exam_detail = new ExaminationDetail();
        $exam_detail = $exam_detail->where(['id' => $param['id']])->find();
        if (!$exam_detail) {

        }

        $exam_detail->answer = $param['answer'];
        $exam_detail->isUpdate(true)->save();
        return json_info(true);
    }

    public function submit_question($type)
    {
        // 检查是不是这个人
        $this->check_cookie();
        $error_number = 0;

        // 校验数据  type


        $exam = new Examination();
        $exam = $exam->where(['userId' => $this->userId, 'type' => $type])->find()->toArray();
        if (!$exam) {

        }

        $exam_detail = new ExaminationDetail();
        $exam_detail = $exam_detail->where(['userId' => $this->userId, 'examId' => $exam['examId'], 'type'=>$type])->select()->toArray();
        if (!$exam_detail) {

        }

        for ($i = 0; $i < count($exam_detail); $i++) {

            $problem_detail = new ProblemDetail();
            $problem_detail = $problem_detail->where(['id' => $exam_detail[$i]['problemDetailId']])->find()->toArray();

            // 错误
            if (strtoupper($exam_detail[$i]['answer']) != $problem_detail['optionRight']) {

                //插入错误集
                $mistake = new Mistake();
                $mistake = $mistake->where(['userId' => $this->userId, 'problemId' => $exam_detail[$i]['problemDetailId'], 'state' => '0'])->find();
                if ($mistake) {
                    $mistake->num = $mistake->num + 1;
                    $mistake->answer = strtoupper($exam_detail[$i]['answer']);
                    $mistake->isUpdate(true)->save();
                } else {
                    $mistake = new Mistake();
                    $mistake->userId = $this->userId;
                    $mistake->problemId = $exam_detail[$i]['problemDetailId'];
                    $mistake->answer = strtoupper($exam_detail[$i]['answer']);
                    $mistake->num = 1;
                    $mistake->save();
                }
                $error_number++;
            }
        }

        $score = (config('params.EXAMNUMBER') - $error_number) * config('params.EXAMSCORE');

        $exam = new Examination();
        $exam = $exam->where(['userId' => $this->userId, 'type' => $type])->find();
        if (!$exam) {

        }

        $exam->complete = 0;
        $exam->time = $exam->time + 1;
        $exam->score = $exam->score + $score;
        $exam->isUpdate(true)->save();

        $exam_record = new ExaminationRecord();
        $exam_record->userId = $this->userId;
        $exam_record->score = $score;
        $exam_record->save();

        $data = [
            'score' => $score,
            'right' => 1,
        ];
        return json_info($data);
    }
}
